import axios, {
  type AxiosInstance,
  AxiosError,
  type AxiosRequestConfig,
  type InternalAxiosRequestConfig,
  type AxiosResponse
} from 'axios'
import { ElNotification } from 'element-plus'
import type { Result } from '@/api/dataType'

/**
 * axios配置
 */
const config = {
  baseURL: import.meta.env.VITE_API_URL,
  timeout: 30*1000
}

class RequestHttp {
  service: AxiosInstance

  /**
   * 请求构造函数
   * @param config
   */
  public constructor(config: AxiosRequestConfig) {
    this.service = axios.create(config)
    this.service.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        return config
      },
      (error: AxiosError) => {
        return Promise.reject(error)
      }
    )
    this.service.interceptors.response.use(
      (response: AxiosResponse) => {
        console.log('response>>',response);
        const { data } = response
        if(data.code == 200){
          return data
        }else{
          ElNotification.error(data.message || '未知错误')
        } 
      },
      (error: AxiosError) => {
        console.log('error>>',error);
        const { response, message } = error
        const data = response?.data as Result<object>
        const errMsg = data ? data.message : message
        ElNotification.error(errMsg || '未知错误')
        return Promise.reject(response?.data || error)
      }
    )
  }

  /**
   * get请求
   * @param url
   * @param params
   * @param config
   */
  get<T>(url: string, params?: object, config = {}): Promise<Result<T>> {
    return this.service.get(url, { params, ...config })
  }

  /**
   * post请求
   * @param url
   * @param data
   * @param config
   */
  post<T>(url: string, data?: object, config = {}): Promise<Result<T>> {
    return this.service.post(url, data, config)
  }

  /**
   * put请求
   * @param url
   * @param data
   * @param config
   */
  put<T>(url: string, data?: object, config = {}): Promise<Result<T>> {
    return this.service.put(url, data, config)
  }

  /**
   * delete请求
   * @param url
   * @param data
   * @param config
   */
  delete<T>(url: string,config = {}): Promise<Result<T>> {
    return this.service.delete(url, config)
  }

  /**
   * request请求
   * @param config
   */
  request<T>(config: AxiosRequestConfig): Promise<Result<T>> {
    return this.service.request(config)
  }

  /**
   * 下载文件
   * @param url
   * @param data
   * @param config
   */
  download(url: string, data?: object, config = {}): Promise<BlobPart> {
    return this.service.post(url, data, { ...config, responseType: 'blob' })
  }
}

export default new RequestHttp(config)
